package com.wocai.jrt.orgadmin.product.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.utils.charstr.StringUtils;
import com.vteba.utils.date.DateUtils;
import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.orders.model.Orders;
import com.wocai.jrt.orgadmin.product.model.Product;
import com.wocai.jrt.orgadmin.product.service.spi.RemindService;
import com.wocai.jrt.orgadmin.utils.SmsUtils;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 产品兑付提醒Action
 * @author yinlei
 * @date 2015年12月14日 下午12:02:05
 */
@Controller
@RequestMapping("/remind")
public class RemindAction {
	private static final Logger LOGGER = LoggerFactory.getLogger(RemindAction.class);
	private static final String SMS = "您购买的[{productName}]基金产品将于{endDate}到期，请联系您的专属理财师进行兑付";
	private static final String NAME = "{productName}";
	private static final String DATE = "{endDate}";
	
	
	@Inject
	private RemindService remindServiceImpl;
	
	/**
	 * 跳转到列表页
	 * @return 逻辑视图名
	 */
	@RequestMapping("/list")
	public String list() {
		return "remind/list";
	}

	/**
	 * 执行查询操作
	 * @return 分页结果
	 */
	@ResponseBody
	@RequestMapping("/query")
	public Map<String, Object> query(@RequestBody Product model) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (model.getDuration() == null) {
				model.setDuration(30);
			}
			model.setOrgId(UserUtils.getOrgId());
			List<Product> list = remindServiceImpl.getRemindList(model);
			
			int totalRecords = 0;
			if (list != null && list.size() > 0) {
				totalRecords = remindServiceImpl.getRemindListCount(model);
			}
			
			result.put("aaData", list); // 返回的数据
    		result.put("iTotalRecords", totalRecords); // 总记录数
    		result.put("iTotalDisplayRecords", totalRecords); // 显示的记录数
		} catch (Exception e) {
			LOGGER.error("query cashed reminder error, msg=[{}].", e);
		}
		return result;
	}
	
	@RequestMapping("/detail/{id}")
	public String detail(Product model) {
		return "remind/detail";
	}
	
	/**
	 * 查询待兑付订单明细
	 * @return 分页结果
	 */
	@ResponseBody
	@RequestMapping("/queryDetail")
	public Map<String, Object> queryDetail(@RequestBody Orders model) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			
			model.setOrgId(UserUtils.getOrgId());
			List<Orders> list = remindServiceImpl.getRemindDetailList(model);
			
			int totalRecords = 0;
			if (list != null && list.size() > 0) {
				totalRecords = remindServiceImpl.getRemindDetailListCount(model);
			}
			
			result.put("aaData", list); // 返回的数据
    		result.put("iTotalRecords", totalRecords); // 总记录数
    		result.put("iTotalDisplayRecords", totalRecords); // 显示的记录数
		} catch (Exception e) {
			LOGGER.error("query cashed reminder detail error, msg=[{}].", e);
		}
		return result;
	}
	
	/**
	 * 发送短信，给待兑付订单的投资人发送短信
	 * @return 操作结果
	 */
	@ResponseBody
	@RequestMapping("/sendMessage")
	public JsonBean sendMessage(@RequestBody ArrayList<Orders> list) {
		JsonBean bean = new JsonBean();
		try {
			for (Orders order : list) {
				String mobile = order.getMobile();
				if (StringUtils.isEmpty(mobile)) {
					continue;
				}
				String content = SMS.replace(NAME, order.getProductName())
						.replace(DATE, DateUtils.toDateString(order.getEndDate(), DateUtils.YMD));
				String result = SmsUtils.send(mobile, content);
				if (LOGGER.isInfoEnabled()) {
					LOGGER.info("给机构[{}]用户[{}]发送短信[{}]，发送结果是[{}].", 
							UserUtils.getOrgCode(), mobile, content, result);
				}
			}
			bean.setCode(1);
		} catch (Exception e) {
			bean.setMessage("发送短信错误！");
			LOGGER.error("给待兑付订单的投资人发送短信错误，msg=[{}].", e);
		}
		return bean;
	}
}
